package com.hx.reporter.service.impl;

import com.hx.reporter.domain.User;
import com.hx.reporter.mapper.GroupUserMapper;
import com.hx.reporter.mapper.UserMapper;
import com.hx.reporter.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author ${author}
 * @date 2023-05-28
 */
@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GroupUserMapper groupUserMapper;


    /**
     * 查询用户
     *
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(String id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public List<User> selectUserByGroupId(String groupId) {
        List<String> userIds = groupUserMapper.selectUserIdsByGroupId(groupId);
        List<User> userList=new ArrayList<>();
        for (String userId : userIds) {
            userList.add(userMapper.selectUserById(userId));
        }
        return userList;
    }

    /**
     * 查询用户列表
     *
     * @param user 用户
     * @return 用户
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     *
     * @param user 用户
     * @return 结果
     */
    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户
     *
     * @param user 用户
     * @return 结果
     */
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户
     *
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(String[] ids) {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(String id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public User selectUserByUserNameAndPassword(User user) {
        return userMapper.selectUserByUserNameAndPassword(user.getName(), user.getPassword());
    }
}
